package com.gwz.bus.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gwz.bus.domain.Goods;
import com.gwz.bus.domain.Outport;
import com.gwz.bus.mapper.GoodsMapper;
import com.gwz.bus.mapper.OutportMapper;
import com.gwz.bus.service.OutportService;
import com.gwz.bus.vo.OutportVo;
import com.gwz.sys.utils.DataGridView;

@Service
@Transactional
public class OutportServiceImpl implements OutportService {

	@Autowired
	private OutportMapper outportMapper;

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public DataGridView queryAllOutports(OutportVo outportVo) {
		Page<Object> page = PageHelper.startPage(outportVo.getPage(), outportVo.getLimit());
		List<Outport> data = this.outportMapper.queryAllOutport(outportVo);
		DataGridView view = new DataGridView(page.getTotal(), data);
		return view;
	}

	/**
	 * 添加退货
	 */
	@Override
	public void addOutport(OutportVo outportVo) {
		// 添加退货数据
		this.outportMapper.insertSelective(outportVo);
		// 减少商品库存
		Goods record = this.goodsMapper.selectByPrimaryKey(outportVo.getGoodsid());
		record.setNumber(record.getNumber() - outportVo.getNumber());
		this.goodsMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Outport queryOutportById(Integer id) {

		return this.outportMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateOutport(OutportVo outportVo) {
		this.outportMapper.updateByPrimaryKeySelective(outportVo);
	}

	@Override
	public void deleteOutport(Integer id) {
		Outport outport = outportMapper.selectByPrimaryKey(id);
		// 增加商品库存
		Goods record = this.goodsMapper.selectByPrimaryKey(outport.getGoodsid());
		record.setNumber(record.getNumber() + outport.getNumber());
		this.goodsMapper.updateByPrimaryKeySelective(record);
		//删除退货信息
		this.outportMapper.deleteByPrimaryKey(outport.getId());
	}

}
